import numpy as np
import os
from collections import defaultdict
count_dict = defaultdict(lambda: 0)
count_dict2 = defaultdict(lambda: 0)

# npy_dir = '/home/ddy/projects/emotions/iemocap_4emo_spectrogram'
# npy_dir = '/Users/d/Project/emotions/data/iemocap_4emo_spectrogram'
npy_dir = '/Volumes/DATA_D/iemocap_related/iemocap_4emo_spectrogram_801'
interval = 200


def count_frames(session_name):
    label_filepath = os.path.join(npy_dir, session_name + '_sentence_label')
    with open(label_filepath, 'r') as label_f:
        for line in label_f:
            eles = line.split()
            if len(eles) == 2:
                npy_f = os.path.join(npy_dir, session_name,
                                     eles[0] + '.npy')
                spectr = np.load(npy_f)
                count_dict[spectr.shape[0]] += 1


def main():
    session_names = ['Session1', 'Session2', 'Session3', 'Session4', 'Session5']
    for session_name in session_names:
        count_frames(session_name)
    # count_dict_items = sorted(count_dict.items(), key=lambda x: x[0])
    # print(count_dict_items)
    for frames, c in count_dict.items():
        k = int(frames/interval)
        count_dict2[int(k * interval / 100)] += c
    print(sorted(count_dict2.items(), key=lambda x: x[0]))

    # print(count_dict)


if __name__ == '__main__':
    main()
